<?php
declare (strict_types = 1);

namespace app\common\service;

use think\facade\Request;
use app\common\model\ClothTemp as M;
use app\common\validate\ClothTemp as V;

class ClothTemp
{
    // 添加
    public static function goAdd($data)
    {
        //验证
        $validate = new V;
        if(!$validate->scene('add')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
			$data=self::dataFormat($data,'add');
            M::create($data);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }
    
    // 编辑
    public static function goEdit($data,$id){
    	$where = is_array($id) ? $id : ['id'=>$id];
        //验证
        $validate = new V;
        if(!$validate->scene('edit')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
			$data=self::dataFormat($data,'edit');
             M::update($data,$where);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 状态
    public static function goStatus($data,$id){
        $model = is_array($id) ? M::where($id)->find() : M::find($id);
        if ($model->isEmpty())  return ['msg'=>'数据不存在','code'=>201];
        try{
            $model->save($data);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 删除
    public static function goRemove($id){
	$model = is_array($id) ? M::where($id)->find() : M::find($id);
        if ($model->isEmpty()) return ['msg'=>'数据不存在','code'=>201];
        try{
           $model->delete();
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 批量删除
    public static function goBatchRemove($ids){
        if (!is_array($ids)) return ['msg'=>'数据不存在','code'=>201];
		$model = M::where($ids)->select();
        try{
            $model->delete();
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 获取列表
    public static function goRecycle($where = []){
        if (Request::isPost()){
            $ids = Request::param('ids');
			$where[]=['id','in',$ids];
            if (!is_array($ids)) return ['msg'=>'参数错误','code'=>'201'];
            try{
                if(Request::param('type')){
                    $data = M::onlyTrashed()->where($where)->select();
                    foreach($data as $k){
                        $k->restore();
                    }
                }else{
                    M::destroy($ids,true);
                }
            }catch (\Exception $e){
                return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
            }
            return ['msg'=>'操作成功'];
        }
        //按用户名
        
        $limit = I('get.limit/d');
        
               //按品名查找
               if ($title = input("title")) {
                   $where[] = ["title", "like", "%" . $title . "%"];
               }
               //按编号查找
               if ($number = input("number")) {
                   $where[] = ["number", "like", "%" . $number . "%"];
               }
               //按供应编号查找
               if ($onumber = input("onumber")) {
                   $where[] = ["onumber", "like", "%" . $onumber . "%"];
               }
        $list = M::onlyTrashed()->order(['delete_time'=>'desc'])->where($where)->paginate($limit);
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }
    
    //批量修改分类
    public static function setTypes($data,$uuid){
        $ids=explode('|',Request::post('ids'));
        $where=[
            ['id','in',$ids],
            ['uuid','=',$uuid],
        ];
        try{
            $model = M::where($where)->update(['tid'=>$data['tid']]);
            return ['msg'=>'操作成功'];
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }
	
	//修改某个参数
	public static function setFieldValue($data,$where=[]){
		if(empty($where)){
			$model =  M::find($data['id']);
		}else{
			$model = M::where($where)->find();
		}
		if ($model->isEmpty())  return ['msg'=>'数据不存在','code'=>201];
		try{
			$save_data=[
				$data['field'] => $data['value'],
			];
		    $model->save($save_data);
			return ['msg'=>'操作成功'];
		}catch (\Exception $e){
		    return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
		}
	}
	
	//拷贝产品
	public static function copy($ids,$uuid){
		if(!is_array($ids)) $id=[$ids];
		$i=0;
		foreach($ids as $id){
			$data=M::where(['id'=>$id,'uuid'=>$uuid])->find()->toArray();
			$data['number']=$data['number'].'-CPOY';
			$data['title']='副本 - '.$data['title'];
			$data['update_time']=time();
			$data['create_time']=time();
			unset($data['id']);
			try {
			    M::create($data);
				$i++;
			}catch (\Exception $e){
			    return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
			}
		}
		return ['msg'=>'成功复制 '.$i.' 条数据','data'=>['status'=>1]];
	}
	
	// 预处理添加和修改的数据
	public static function dataFormat($data,$type='add'){
		
		if($type == 'add'){					//添加时才处理
			
		}else if($type == 'edit'){			//修改时才处理
			
		}
		// 无论修改还是添加都处理
		return $data;
	}
}
